﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Drawing;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;

namespace BingImages
{
    class Theme
    {
        // ------------ DEBUT INITIALISATION VARIABLES ------------ //
        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        private static Thread _newThread;
        private static bool _first = true;
        // ------------ FIN INITIALISATION VARIABLES ------------ //

        public static void setTheme(string state, string timeInterval)
        {
            if (state == "True")
            {
                _newThread = new Thread(() => wallpaperScroll(int.Parse(timeInterval)));
                _newThread.IsBackground = true;
                _newThread.Start();

                _first = false;
            }
        }

        private static void wallpaperScroll(int timeInterval)
        {
            Folder folder = new Folder();
            FileInfo[] files = null;

            files = folder.getFiles("theme", "*.png");

            if (files.Length > 0)
            {
                Image picture = null;
                string tempPath = "";
                RegistryKey key = null;
                int totalTime = 0;

                while (true)
                {
                    foreach (FileInfo file in files)
                    {
                        // ------------ DEBUT MODIFICATION FOND ECRAN ------------ //
                        picture = Image.FromFile(Path.GetFullPath(file.FullName));
                        tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
                        picture.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);

                        // On libère la ressource
                        picture.Dispose();

                        key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
                        key.SetValue(@"WallpaperStyle", 2.ToString());
                        key.SetValue(@"TileWallpaper", 0.ToString());

                        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
                        // ------------ FIN MODIFICATION FOND ECRAN ------------ //

                        totalTime = timeInterval * 1000;

                        Thread.Sleep(totalTime);
                    }
                }
            }
        }

        public static void abortThread()
        {
            if (!_first)
            {
                if (_newThread.IsAlive)
                {
                    _newThread.Abort();
                }
            }
        }
    }
}
